<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片区域 -->
        <el-card>
            <el-row>
                <el-col :span="8">
                    <el-input>
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <el-table :data="orderList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="订单编号" prop="order_number"></el-table-column>
                <el-table-column label="订单价格" prop="order_price"></el-table-column>
                <el-table-column label="付款状态">
                    <template slot-scope="scope">
                        <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
                        <el-tag type="success" v-else>已付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="是否发货" prop="is_send"></el-table-column>
                <el-table-column label="订单时间" prop="create_time">
                    <template slot-scope="scope">
                        {{ scope.row.create_time | dataFormat }}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template>
                        <el-button type="primary" size="mini" title="编辑订单" icon="el-icon-edit" @click="showEditDialog"></el-button>
                        <el-button type="success" size="mini" title="查看物流" icon="el-icon-location" @click="showLocationDialog"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </el-card>

        <!-- 编辑的对话框 -->
        <el-dialog title="地址" :visible.sync="editDialogVisible" width="30%" @close="editDialogClosed">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef">
                <el-form-item label="省级联动" prop="selectedCityKey">
                    <el-cascader v-model="editForm.selectedCityKey" :options="cityList" expand-trigger></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址" prop="address">
                    <el-input v-model="editForm.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 物流的对话框 -->
        <el-dialog title="物流" :visible.sync="locationDialogVisible" width="50%" @close="locationDialogClosed">
            <el-timeline>
                <el-timeline-item v-for="(item, index) in locationList" :key="index" :timestamp="item.time">
                    {{item.context}}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import citydata from './citydata.js'

export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            orderList: [],
            total: 0,
            editDialogVisible: false,
            cityList: citydata,
            editForm: {
                selectedCityKey: [],
                address: ''
            },
            editFormRules: {
                selectedCityKey: [
                    { required: true, message: '必填', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '必填', trigger: 'blur' }
                ]
            },
            locationDialogVisible: false,
            locationList: []
        }
    },
    created() {
        this.getOrderList();
    },
    methods: {
        async getOrderList() {
            const { data: res } = await this.$http.get('orders', {
                params: this.queryInfo
            });

            if(res.meta.status !== 200) {
                return this.$message.error(res.meta.msg);
            }

            this.$message.success(res.meta.msg);
            this.orderList = res.data.goods;
            this.total = res.data.total;
        },
        showEditDialog() {
            this.editDialogVisible = true;
        },
        editDialogClosed() {
            this.$refs.editFormRef.resetFields();
        },
        handleSizeChange(pagesize) {
            this.queryInfo.pagesize = pagesize;
            this.getOrderList();
        },
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getOrderList();
        },
        showLocationDialog() {
            this.getLocationInfo();
            this.locationDialogVisible = true;
        },
        locationDialogClosed() {},
        async getLocationInfo() {
            const { data: res } = await this.$http.get('/kuaidi/804909574412544580');

            if(res.meta.status !== 200) {
                return this.$message.error(res.meta.msg);
            }

            this.$message.success(res.meta.msg);
            this.locationList = res.data;
        }
    }
}
</script>

<style lang="less">

</style>